class ListNode {
    int val;
    ListNode next;ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }

class Solution {
    public ListNode middleNode(ListNode head) {
        if (head == null){
            return null;
        }
        if (head.next == null ){
            return head;
        }

        ListNode S = head;
        ListNode Q = head;
        while(Q.next!=null){
            if(Q.next.next==null){
                return S.next;
            }
            Q=Q.next.next;
            S=S.next;
        }
        return S;
    }
}


public class Text1 {
}
